#!/usr/bin/env python
# coding: utf-8

# In[ ]:


import requests,json
import pandas as pd
# import ipywidgets as widgets
key1_key = "8066ec0cc0d0913d5d385f8693a88128"

def geocode(key,address):
    geocode_url = "https://restapi.amap.com/v3/geocode/geo?parameters"
    params = {
        "key":key,
        "address":address
    }
    response = requests.get(url=geocode_url,params=params)
    results = response.json()['geocodes'][0]['location']

    return results

def regeocode(key,location):
    #逆地理编码
    regeocode_url = "https://restapi.amap.com/v3/geocode/regeo?parameters"
    params = {
        "key":key,
        "location":location,
        "output":"json"
}
    response = requests.get(regeocode_url,params=params)
    results = response.json()['regeocode']['formatted_address']
    return results

def walk_path(key,origin,destination):
 #步行路线规划
    walk_url = "https://restapi.amap.com/v3/direction/walking?parameters"
    params = {
    "key":key,
    "origin" : origin,
    "destination" : destination,
    "output":"json"
    }
    response = requests.get(url=walk_url,params=params)
    walk_path = response.json()['route']['paths'][0]['steps']

    return walk_path


def bus_path(key,origin,destination,city):
#公交车路线规划
    bus_url = "https://restapi.amap.com/v3/direction/transit/integrated?parameters"
    params = {
    "key":key,
    "origin" : origin,
    "destination" : destination,
    "city":city,
    "output":"json"
    }
    response = requests.get(url=bus_url,params=params)
    bus_path = response.json()

    return bus_path

def car_path(key,origin,destination):
#驾车路径规划
    car_url = "https://restapi.amap.com/v3/direction/driving?parameters"
    params = {
    "key":key,
    "origin" : origin,
    "destination" : destination,
    "output":"json"
    }
    response = requests.get(url=car_url,params=params)
    car_path = response.json()

    return car_path

def bike_path(key,origin,destination):
#骑行路线规划
    bike_url = "https://restapi.amap.com/v4/direction/bicycling?parameters"
    params = {
    "key":key,
    "origin" : origin,
    "destination" : destination,
    "output":"json"
    }
    response = requests.get(url=bike_url,params=params)
    bike_path = response.json()['data']['paths'][0]['steps']

    return bike_path

def distance(key,origin,destination):
#测量路径规划
    distance_url = "https://restapi.amap.com/v3/distance?parameters"
    params = {
    "key":key,
    "origins" : origin,
    "destination" : destination,
    "output":"json"
    }
    response = requests.get(url=distance_url,params=params)
    distance = response.json()['results'][0]['distance']

    return distance

def adm_region(key,keywords):
    #行政区域查询
    adm_region_url = "https://restapi.amap.com/v3/config/district?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "output":"json"
}

    response = requests.get(url=adm_region_url,params=params)
    adm_region = response.json()['districts'][0]['districts']
    return adm_region

def search_keywords(key,keywords):
    #关键字搜索
    keywords_url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "output":"json"
}

    response = requests.get(url=keywords_url,params=params)
    search_keywords = response.json()
    return search_keywords

def search_near(key,location):
    #周边搜索
    near_url = "https://restapi.amap.com/v3/place/around?parameters"
    params = {
        "key":key,
        "location":location,
        "output":"json"
}

    response = requests.get(url=near_url,params=params)
    search_near = response.json()['pois']
    return search_near

def search_polygon(key,polygon):
    #多边形搜索
    polygon_url = "https://restapi.amap.com/v3/place/polygon?parameters"
    params = {
        "key":key,
        "polygon":polygon,
        "output":"json"
}

    response = requests.get(url=polygon_url,params=params)
    search_polygon = response.json()['pois']
    return search_polygon

def search_ID(key,id):
    #ID搜索
    ID_url = "https://restapi.amap.com/v3/place/text?parameters"
    params = {
        "key":key,
        "id":id,
        "output":"json"
}

    response = requests.get(url=ID_url,params=params)
    search_ID = response.json()
    return search_ID

def loc_IP(key,ip):
    #IP定位
    IP_url = "https://restapi.amap.com/v3/ip?parameters"
    params = {
        "key":key,
        "ip":ip,
        "output":"json"
}

    response = requests.get(url=IP_url,params=params)
    loc_IP = response.json()
    return loc_IP

def transform(key,locations):
    transform_url = "https://restapi.amap.com/v3/assistant/coordinate/convert?parameters"
    params = {
        "key":key,
        "locations":locations,
        "coordsys":"mapbar",
        "output":json
}

    response = requests.get(url=transform_url,params=params)
    transform = response.json()
    return transform

def weather(key,city):
    #天气查询
    weather_url = "https://restapi.amap.com/v3/weather/weatherInfo?parameters"
    params = {
        "key":key,
        "city":city,
        "extensions":"all",
        "output":json
}

    response = requests.get(url=weather_url,params=params)
    weather = response.json()
    return weather

def note(key,keywords):
    #搜索提示
    note_url = "https://restapi.amap.com/v3/assistant/inputtips?parameters"
    params = {
        "key":key,
        "keywords":keywords,
        "output":json
}

    response = requests.get(url=note_url,params=params)
    note = response.json()
    return note